package com.leyou.user.controller;

import com.leyou.common.exception.LyException;
import com.leyou.user.entity.User;
import com.leyou.user.serivce.UserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.stream.Collectors;

/**
 * @author LT
 * @version 1.0.0
 * @ClassName UserController.java
 * @Description TODO
 * @createTime 2020年05月15日 19:27:00
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 数据校验
     *
     * @param data 要校验的数据
     * @param type 要校验的数据类型：1，用户名；2，手机
     * @return
     */
    @GetMapping("/check/{data}/{type}")
    @ApiOperation(value = "校验用户名数据是否可用")
    @ApiResponses({
            @ApiResponse(code = 200, message = "校验结果有效，true或false代表可用或不可用"),
            @ApiResponse(code = 400, message = "请求参数有误，比如type不是指定值")
    })
    public ResponseEntity<Boolean> checkData(
            @ApiParam(value = "要校验的数据", example = "xiaofeifei")
            @PathVariable("data") String data,
            @ApiParam(value = "数据类型，1：用户名，2：手机号", example = "1")
            @PathVariable("type") Integer type) {
        Boolean flag = userService.checkData(data, type);
        return ResponseEntity.ok(flag);
    }

    /**
     * 发送短信验证码
     *
     * @param phone 手机号码
     * @return
     */
    @ApiOperation(value = "发送短信验证码")
    @PostMapping("/code")
    public ResponseEntity<Void> sendSms(@RequestParam("phone") String phone) {
        userService.sendSms(phone);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 注册用户
     *
     * @param user
     * @param code
     * @return
     */
    @ApiOperation(value = "用户注册接口")
    @PostMapping("/register")
    public ResponseEntity<Void> register(@Valid User user, BindingResult bindingResult, @RequestParam("code") String code) {
        // 手动从user中取出值来进行判空等校验： 服务器端校验
        if (bindingResult.hasErrors()) {
            // 如果有异常，收集异常信息
            String errors = bindingResult.getAllErrors()
                    .stream()
                    .map(ObjectError::getDefaultMessage)
                    .collect(Collectors.joining("|"));
            throw new LyException(400, errors);
        }

        userService.register(user, code);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据用户名和密码查询用户信息
     *
     * @param username
     * @param password
     * @return
     */
    @ApiOperation(value = "根据用户名和密码查询用户信息")
    @GetMapping("/query")
    public ResponseEntity<User> findUserByNameAndPassword(
            @RequestParam("username") String username,
            @RequestParam("password") String password
    ) {
        User user = userService.findUserByNameAndPassword(username, password);
        return ResponseEntity.ok(user);
    }
}
